package com.authine.cloudpivot.ext.utils.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ServiceUrlConst;
import com.authine.cloudpivot.ext.dto.system.OrganizationDto;
import com.authine.cloudpivot.ext.dto.system.RoleDto;
import com.authine.cloudpivot.ext.enums.system.ArchitectureCateEnum;
import com.authine.cloudpivot.ext.utils.HttpClients;
import com.authine.common.util.ObjectMapperUtils;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import com.authine.mvp.app.org.dto.domainmodel.UnitType;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName RoleUtils
 * @Description: 角色工具类，方便调用
 * @Author fj
 * @Date 2021/8/23
 * @Version V1.0
 **/
public class RoleUtils {
    /**
     * 根据组织id和角色名称判断角色是否存在
     *
     * @param orgId    组织id
     * @param roleName  角色名称
     * @return 用户信息
     */
    public static boolean existByOrgIdAndRoleName(String orgId, String roleName) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.EXIST_BY_ORG_ID_AND_ROLE_NAME_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        // 通过用户id获取用户信息
        if (!StringUtils.isEmpty(orgId)) {
            OrganizationDto orgByOrgId = OrgUtils.getOrgByOrgId(orgId);
            SelectorFormat org = new SelectorFormat();
            org.setName(orgByOrgId.getName());
            org.setId(orgByOrgId.getId());
            org.setType(UnitType.DEPARTMENT.getIndex());
            userMap.put("organization_id", Collections.singletonList(org));
        } else {
            userMap.put("organization_id", Lists.newArrayList());
        }
        userMap.put("name", roleName);
        return HttpClients.execute(url, userMap, Boolean.class);
    }

    /**
     * 根据用户id和角色名称判断角色是否存在
     *
     * @param userId    用户id
     * @param roleName  角色名称
     * @return 用户信息
     */
    public static boolean existByUserIdAndRoleName(String userId, String roleName) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.EXIST_BY_USER_ID_AND_ROLE_NAME);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("id", userId);
        userMap.put("roleName", roleName);
        return HttpClients.execute(url, userMap, Boolean.class);
    }

    /**
     * 根据组织id和角色名称获取角色列表
     *
     * @param orgId         组织id
     * @param roleNames     角色名称列表
     * @return 用户信息
     */
    public static List<RoleDto> getRolesByOrgIdAndNames(String orgId, List<String> roleNames) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ROLES_BY_ORG_ID_AND_NAMES_URL);
        OrganizationDto orgByOrgId = OrgUtils.getOrgByOrgId(orgId);
        SelectorFormat org = new SelectorFormat();
        org.setName(orgByOrgId.getName());
        org.setId(orgByOrgId.getId());
        org.setType(UnitType.DEPARTMENT.getIndex());
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("organization_id", Collections.singletonList(org));
        userMap.put("names", roleNames);
        String execute = HttpClients.execute(url, userMap);
        return ObjectMapperUtils.readValue(execute, RoleDto.class);
    }

    /**
     * 根据组织id获取所有管理员角色架构
     *
     * @param orgId   组织id
     * @return 管理员角色集合
     */
    public static Set<ArchitectureCateEnum> getOrganizationArchitecture(String orgId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORGANIZATION_ARCHITECTURE_URL);
        OrganizationDto orgByOrgId = OrgUtils.getOrgByOrgId(orgId);
        SelectorFormat org = new SelectorFormat();
        org.setName(orgByOrgId.getName());
        org.setId(orgByOrgId.getId());
        org.setType(UnitType.DEPARTMENT.getIndex());
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("organization_id", Collections.singletonList(org));
        String execute = HttpClients.execute(url, userMap);
        return JSON.parseObject(execute, new TypeReference<Set<ArchitectureCateEnum>>() {
        });
    }

    /**
     * 根据角色id列表获取角色名称
     *
     * @param roleIDs    角色id
     * @return 用户信息
     */
    public static Set<String> listRoleNames(List<String> roleIDs) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.LIST_ROLE_NAMES_URL);
        Map<String, Object> roleMap = Maps.newHashMap();
        roleMap.put("ids", roleIDs);
        String execute = HttpClients.execute(url, roleMap);
        return JSON.parseObject(execute, new TypeReference<Set<String>>() {
        });
    }

    /**
     * 刷新资源数据
     *
     * @param appCode    应用编码
     */
    public static void refreshResource(String appCode) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.REFRESH_RESOURCE_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("appCode", appCode);
        HttpClients.execute(url, userMap);
    }
}
